//
// Created by admin on 2024/5/9.
//

#ifndef _PINGPONG_BUFFER_H
#define _PINGPONG_BUFFER_H

#include <stdint.h>

class PingPongBuffer {
public:
    typedef struct {
        uint8_t  *data[2];
        uint32_t size;

        volatile uint8_t writeIndex;
        volatile uint8_t readIndex;
        volatile uint8_t available[2];
    } Buffer_t;

public:
    explicit PingPongBuffer(uint32_t size);

    ~PingPongBuffer();

    void WriteData(void *data, uint32_t size = UINT32_MAX);
    bool ReadData(void *data, uint32_t size = UINT32_MAX);

    bool GetReadBuff(void **pReadBuf);
    void ReadDone();
    Buffer_t *GetCache();

    uint32_t Size() { return buffer.size; }

    bool Available() { return (buffer.available[0] != false && buffer.available[1] != false); }

private:
    Buffer_t buffer;

};


#endif //_PINGPONG_BUFFER_H
